//
//  SFSwipeTableView.m
//  Test
//
//  Created by xueshan1 on 2019/7/17.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "SFSwipeTableView.h"
#import "UIView+Externsion.h"

static NSString * const SwipeItemContentOffset = @"contentOffset";

@interface SFSwipeTableView ()<UIScrollViewDelegate,SFSwipeHeaderViewDelegate>

@property(nonatomic,strong)UIScrollView *contentScrollView;
@property(nonatomic,strong)UITableView *currentItemView;

@property(nonatomic,strong)SFSwipeHeaderView *topHeaderView;
@property(nonatomic,assign)CGFloat topAllInset;
@property(nonatomic,assign)CGFloat headerTopInset;
@property(nonatomic,assign)CGFloat segmentTopInset;

@end

@implementation SFSwipeTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)setupUI{
    //default
 
    [self addSubview:self.contentScrollView];

    [self reloadView];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetX = scrollView.contentOffset.x;
    if (scrollView == self.contentScrollView) {
        NSInteger index = offsetX / self.width;
        UITableView *itemView = self.itemViews[index];
        if (_currentItemView != itemView) {
            [_currentItemView removeObserver:self forKeyPath:SwipeItemContentOffset];
            _currentItemView = itemView;
            [_currentItemView addObserver:self forKeyPath:SwipeItemContentOffset options:NSKeyValueObservingOptionNew context:nil];
            
            if (self.delegate && [self.delegate respondsToSelector:@selector(swipeTableViewCurrentItemViewChanged:currentIndex:)]) {
                [self.delegate swipeTableViewCurrentItemViewChanged:_currentItemView currentIndex:index];
            }
        }

    }
    
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:SwipeItemContentOffset]) {
        CGFloat offsetY = [change[NSKeyValueChangeNewKey] CGPointValue].y;
        NSLog(@"tableview:%ld偏移:%f",(long)[(UITableView *)object tag] ,offsetY);
        
        if (offsetY > -_segmentTopInset) {
            //继续向上, segment固定
            self.topHeaderView.bottom = _segmentTopInset;

        }else{
            //segment在移动
            self.topHeaderView.bottom = -offsetY;
            //同步其他tableview
            for (UITableView *otherTableView in self.itemViews) {
                if (otherTableView.tag != self.currentItemView.tag) {
//                    NSLog(@"other:%ld   cur:%ld ",otherTableView.tag, self.currentItemView.tag);
                    otherTableView.contentOffset = CGPointMake(0, offsetY);
                }
            
            }
            

        }
        
    }
    
}

- (void)scrollToItemViewAtIndex:(NSInteger)index animated:(BOOL)animated{
    NSAssert(index <= self.itemViews.count - 1, @"segmentBar index越界");
    [self.contentScrollView setContentOffset:CGPointMake(self.width * index, self.contentScrollView.contentOffset.y) animated:animated];
    
}
#pragma mark - header delegate
- (CGFloat)minY_HeaderViewFrame{
    CGFloat minY_offScreenHeight = -((self.currentItemView.contentSize.height + _topAllInset) - self.currentItemView.height);
    if (minY_offScreenHeight > 0) {//contentSize < bounds
        return 0;
    }
    return minY_offScreenHeight;
}
- (CGFloat)maxY_HeaderViewFrame{
    return 0;
}
- (void)swpipeHeaderViewDidFrameChanged:(SFSwipeHeaderView *)headerView{
    CGFloat bottom = headerView.bottom;
    self.currentItemView.contentOffset = CGPointMake(0, -bottom);
    
}
- (void)swipeHeaderViewIsDragging:(BOOL)isDragging{

}

#pragma mark - set get
- (UIScrollView *)contentScrollView{
    if (!_contentScrollView) {
        _contentScrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
        _contentScrollView.backgroundColor = [UIColor clearColor];
        _contentScrollView.showsHorizontalScrollIndicator = NO;
        _contentScrollView.showsVerticalScrollIndicator = NO;
        _contentScrollView.pagingEnabled = YES;
        _contentScrollView.delegate = self;
        
    }
    return _contentScrollView;
}

- (SFSwipeHeaderView *)topHeaderView{
    if (!_topHeaderView) {
        _topHeaderView = [[SFSwipeHeaderView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, _topAllInset)];
        _topHeaderView.delegate = self;
        _topHeaderView.backgroundColor = [UIColor redColor];
        [self addSubview:_topHeaderView];
        
    }
    return _topHeaderView;
}


- (void)setItemViews:(NSArray<UITableView *> *)itemViews{
    _itemViews = itemViews;
    if (itemViews.count > 0) {
        _contentScrollView.contentSize = CGSizeMake(self.width * _itemViews.count, self.height);
        for (int i = 0; i <itemViews.count; i++) {
            UITableView *tb  = itemViews[i];
            tb.tag = i;
            tb.x = i * self.width;
            tb.contentInset = UIEdgeInsetsMake(_topAllInset, 0, 0, 0);
            tb.scrollIndicatorInsets = UIEdgeInsetsMake(_topAllInset, 0, 0, 0);
            tb.contentOffset = CGPointMake(0, -_topAllInset);
             [_contentScrollView addSubview:tb];
        }
        _currentItemView = itemViews[0];
        _currentItemView.tag = [itemViews[0] tag];
        [_currentItemView addObserver:self forKeyPath:SwipeItemContentOffset options:NSKeyValueObservingOptionNew context:nil];
    }
    
    
}
- (void)setTopAllInset:(CGFloat)topAllInset{
    _topAllInset = topAllInset;
    if (self.itemViews.count > 0) {
        for (int i = 0; i <self.itemViews.count; i++) {
            UITableView *tb  = self.itemViews[i];
            tb.contentInset = UIEdgeInsetsMake(_topAllInset, 0, 0, 0);
            tb.scrollIndicatorInsets = UIEdgeInsetsMake(_topAllInset, 0, 0, 0);
            tb.contentOffset = CGPointMake(0, -_topAllInset);
        }
    }
}
- (void)setHeaderView:(UIView *)headerView{
    _headerView = headerView;
    _headerTopInset = headerView.height;
    [self reloadView];
}
- (void)setSegmentBarView:(UIView *)segmentBarView{
    _segmentBarView = segmentBarView;
    _segmentTopInset = segmentBarView.height;
    [self reloadView];
}

- (void)reloadView{
    self.topAllInset = _headerTopInset + _segmentTopInset;
    
    self.topHeaderView.height = self.topAllInset;
    self.topHeaderView.y = 0;
    _headerView.y = 0;
    [self.topHeaderView addSubview:_headerView];
    _segmentBarView.y = _headerView.bottom;
    [self.topHeaderView addSubview:_segmentBarView];

}



@end
